//
//  FoodTherapyViewController.m
//  project_delicious
//
//  Created by dlios on 15-3-23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FoodTherapyViewController.h"
#import "FoodTherapyCell.h"
#import "DiseaseViewController.h"


@interface FoodTherapyViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation FoodTherapyViewController

- (void)dealloc
{
    [_hud release];
    [_tableView release];
    [_arr release];
    [super dealloc];
}
- (NSURL *)strChangeUrl:(NSString *)str
{
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    return url;
}
- (void)dataHandler:(NSString *)str
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arr = [responseObject objectForKey:@"data"];
        NSLog(@"%@", self.arr);
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
        self.tableView .dataSource = self;
        self.tableView .delegate = self;
        [self.view addSubview:self.tableView];
        self.tableView.backgroundColor = [UIColor clearColor];
        [_tableView release];
        [_tableView registerClass:[FoodTherapyCell class] forCellReuseIdentifier:@"xun"];
        [_hud removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"发生错误: %@", error);
    }];
}
//转菊花
- (void)mbHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    self.hud.labelText = @"请稍后";
    self.hud.detailsLabelText = @"正在为您加载";
    [self.view addSubview:self.hud];
    [_hud release];
    [_hud show:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.tabBar.hidden = YES;
    [self mbHud];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"QQ20150406-1.png"]];
    NSString *secStr = @"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tbloffice!getOffice.do?phonetype=2&is_traditional=0";
    [self dataHandler:secStr];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodTherapyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xun"];
    NSString *str = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"officeName"];
    cell.mainLabel.text = str;
    
    NSString *str2 = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"diseaseNames"];
    cell.detailLabel.text = str2;
    
    NSString *str3 = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"imagePath"];
    NSURL *url = [self strChangeUrl:str3];
    [cell.picImageView setImageWithURL:url];
    cell.picImageView.contentMode = UIViewContentModeScaleAspectFit;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiseaseViewController *disease = [[DiseaseViewController alloc] init];
    NSInteger officeid = [[[self.arr objectAtIndex:indexPath.row] objectForKey:@"officeId"] integerValue];
    NSString *str = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tbldisease!getDisease.do?officeId=%ld&is_traditional=0", officeid];
    disease.str = str;
    
   
    
    [self.navigationController pushViewController:disease animated:YES];
    [disease release];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 8;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
